import api from "../../api/api";
import cateDataList from "../../data/cate";
import { category } from "../../api/conf";
import { searCate } from "../../api/conf";
import { search } from "../../api/conf";
import { userList } from "../../api/conf";
import { userDelete } from "../../api/conf";
import { authUserBatchDel } from "../../api/conf";
import { authUserExport } from "../../api/conf";
import { attachMainUpload } from "../../api/conf";
import { authUserImportData } from "../../api/conf";
Component({
  /**
   * 页面的初始数据
   */
  data: {
    curIndex: 0,
    list: [],
    cateId: null,
    dataList: [],
    page: 1,
    reTurn: false,
    searchPage: false,
    searchContent: [],
    searpage: 1,
    Qvaule: null,
    frist: false,
    searclosebtn: false,
    num: 0,
    orderBy: 0,
    cateName: "",
    subCate: [],
    IdSelectMap: {},
    columnList: ["用户名", "部门名"],
  },
  lifetimes: {
    attached: function () {},
  },

  attached: function () {},
  ready: function () {
    this.onLoad();
  },

  methods: {
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (option) {
      var that = this;
      if (this.data.reTurn) {
        return;
      }
      wx.showLoading({
        title: "加载中...",
        mask: true,
      });
      api
        .post(userList, {
          page: that.data.page,
        })
        .then((res) => {
          var dataList = res.data.content;
          //全部不勾选
          for (var i = 0; i < dataList.length; i++) {
            var data = dataList[i];
            data["select"] = false;
          }
          that.setData({
            dataList: dataList,
          });
          wx.hideLoading();
        });
    },
    //新增
    addData(e) {
      //跳转页面
      wx.navigateTo({
        url: "/pages/sys-auth-user-add/sys-auth-user-add",
      });
    },

    //导入Excel，上传附件+插入数据
    upload(e) {
      //附件Id
      var fdAttachId = "";
      //扩展
      var extend = "skip";
      wx.chooseMedia({
        success(res) {
          const tempFilePath = res.tempFiles[0].tempFilePath;
          wx.uploadFile({
            filePath: tempFilePath,
            name: "file",
            url: "http://www.tianluen.net/data/attach/main/upload",
            success(res) {
              var data = JSON.parse(res.data);
              fdAttachId = data.data.fdId;

              api
                .post(authUserImportData, {
                  fdAttachId: fdAttachId,
                  extend: "skip",
                })
                .then((res) => {
                  console.log(res);
                  wx.hideLoading();
                });
            },
          });
        },
      });
    },

    selected(e) {
      console.log(e);
      var fdId = e.target.dataset.id;
      var select = e.target.dataset.select;
      select = !select;
      console.log(select);
      var dataList = this.data.dataList;
      for (var i = 0; i < dataList.length; i++) {
        var data = dataList[i];

        if (fdId === data["fdId"]) {
          console.log("fdId,select", fdId, select);
          data["select"] = select;
        }
      }

      this.setData({
        dataList: dataList,
      });
    },

    //删除
    delete(e) {
      var dataList = this.data.dataList;
      var fdIds = [];
      for (var i = 0; i < dataList.length; i++) {
        var data = dataList[i];
        if (data["select"]) {
          fdIds.push(data["fdId"]);
        }
      }
      console.log("fdIds", fdIds);
      if (fdIds.length == 0) {
        return;
      }
      api
        .post(authUserBatchDel, {
          fdIds: fdIds,
        })
        .then((res) => {
          wx.hideLoading();
        });
    },
    //详情
    dataDetail(e) {
      var fdId = e.currentTarget.dataset.id;

      wx.navigateTo({
        url: "/pages/sys-auth-user-detail/sys-auth-user-detail?fdId=" + fdId,
      });
    },
    dataUpdate(e) {
      var fdId = e.currentTarget.dataset.id;

      wx.navigateTo({
        url: "/pages/sys-auth-user-edit/sys-auth-user-edit?fdId=" + fdId,
      });
    },
    //导出Excel
    export(e) {
      var dataList = this.data.dataList;
      var fdIds = [];
      for (var i = 0; i < dataList.length; i++) {
        var data = dataList[i];
        if (data["select"]) {
          fdIds.push(data["fdId"]);
        }
      }
      console.log("fdIds", fdIds);
      if (fdIds.length == 0) {
        return;
      }
      console.log("authUserExport", authUserExport);
      api
        .post(authUserExport, {
          fdIds: fdIds,
        })
        .then((res) => {
          wx.hideLoading();
        });
    },
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
